package com.wanxi.array.management;

import com.wanxi.array.Student;

import javax.swing.text.Style;
import java.util.ArrayList;
import java.util.Scanner;

public class ManageSystem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<Student> arr = new ArrayList<>();
        while (true) {
            System.out.println("----欢迎来到学生管理系统----");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查询学生");
            System.out.println("5 退出");
            System.out.println("请输入你的选择:");
            int num = scanner.nextInt();
            switch (num) {
                case 1:
                    add(arr);
                    break;
                case 2:
                    remove(arr);
                    break;
                case 3:
                    set(arr);
                    break;
                case 4:
                    find(arr);
                    break;
                case 5:
                    System.exit(0);
                    break;
                default:
                    System.out.println("请不要乱输入");
                    break;
            }
        }
    }

    private static void find(ArrayList<Student> arr) {
        for (int i = 0; i < arr.size(); i++) {
            System.out.println(arr.get(i));
        }
    }

    private static void set(ArrayList<Student> arr) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要修改学生的索引");
        int index = scanner.nextInt();
        System.out.println("请输入需要修改的学生名称");
        String name = scanner.next();
        System.out.println("请输入需要修改的学生年龄");
        String ageStr = scanner.next();
//        如果得到的是空，则表示用户不愿意修改以前的年龄，
//        那么需要拿到以前的年龄
        int age = ageStr.equals("") ? arr.get(index).getAge() : Integer.valueOf(ageStr);
        arr.set(index, new Student(name, age));
    }

    private static void remove(ArrayList<Student> arr) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要删除的学生名称");
        String name = scanner.next();
        for (int i = 0; i < arr.size(); i++) {
            if (arr.get(i).getName().equals(name)) {
                arr.remove(arr.get(i));
                return;
            }
        }
        System.out.println("没有找到你要删除的学生");
    }

    private static void add(ArrayList<Student> arr) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学生姓名");
        String name = scanner.next();
//            scanner.nextLine();
        System.out.println("请输入学生年龄");
        int age = scanner.nextInt();
        arr.add(new Student(name, age));
    }
}
